package cn.itsource.sender;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.junit.Test;

public class MessageSender {

    //发送消息
    @Test
    public void send(){
        //1.创建生产者
        DefaultMQProducer sender = new DefaultMQProducer("sender");
        //2.设置nameserver地址
        sender.setNamesrvAddr("101.35.235.40:9876");
        //3.启动生产者
        try {
            sender.start();
            //4.创建消息对象
            Message message = new Message("topic-order2","tags-order","一个订单消息".getBytes("utf-8"));
            //5.发送消息拿到结果
            SendResult result = sender.send(message);

            //单向消息：没法判断消息是否投递成功，用在允许消息丢失的场景
            sender.sendOneway(message);


            //异步消息
            sender.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {

                }

                @Override
                public void onException(Throwable e) {

                }
            });
            System.out.println(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
